package com.framework.admin.controller;

import com.framework.admin.domain.param.ModuleFormParam;
import com.framework.admin.domain.vo.LoadModuleDataVo;
import com.framework.admin.model.ModuleForm;
import com.framework.admin.service.IModuleFormService;
import com.framework.common.domain.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @description: 模块表单管理 Service
 * @author: lizurong
 * @version: 1.0
 * @date: 2021-03-08 13:07:55
 */
@RestController
@RequestMapping("/moduleform")
@Api(value = "ModuleForm", tags = "模块表单管理")
public class ModuleFormController {
    @Autowired
    private IModuleFormService baseService;

    @ResponseBody
    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public ResultData add(@RequestBody ModuleForm param) {
        baseService.save(param);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "删除")
    @PostMapping(value = "/del")
    public ResultData delete(@RequestBody List<String> ids) {
        baseService.delete(ids);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "编辑")
    @PostMapping(value = "/edit")
    public ResultData edit(@RequestBody ModuleForm param) {
        baseService.update(param);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "更新表单json")
    @PostMapping(value = "/updateFormJson")
    public ResultData updateFormJson(@RequestBody ModuleForm param) {
        baseService.updateFormJson(param.getFormJson(), param.getId());
        return ResultData.success();
    }

    @ApiOperation("获取分页")
    @PostMapping(value = "/listPage")
    public ResultData listPage(@RequestBody(required = false) ModuleFormParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNo(), param.getPageSize());
        Page<ModuleForm> users = baseService.listPage(param, pageRequest);
        return ResultData.data(users);
    }

    @ApiOperation("获取数据库表列表")
    @PostMapping(value = "/tableList")
    public ResultData tableList() {
        List<String> tableNameAndDescList = baseService.tableList();
        return ResultData.data(tableNameAndDescList);
    }

    @ApiOperation("加载模块数据")
    @PostMapping(value = "/loadModuleData")
    public ResultData loadModuleData(@RequestParam String id) {
        LoadModuleDataVo loadModuleDataVo = baseService.loadModuleData(id);
        return ResultData.data(loadModuleDataVo);
    }
}
